﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using Highchart.Core.Appearance;
using Highchart.Core.Data.Chart;
using Highchart.Core;
using Highchart.Core.Events;
using Newtonsoft.Json;

namespace Highchart.Web
{
    public partial class Sample10 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Exemplo10();
        }

        private void Exemplo10()
        {
            
            //Define the axes
            hcVendas.YAxis.Add(new YAxisItem {title = new Title("Faturamento")});
            hcVendas.XAxis.Add(new XAxisItem { type = AxisDataType.datetime, dateTimeLabelFormats = new DateTimeLabelFormats() { day = "%e of %b" } });

            hcVendas.Tooltip = new ToolTip("Highcharts.dateFormat('%e. %b %Y', this.x) +': '+ this.y");

            //Get point collection
            PointCollection _pointCollection = new PointCollection();
            DataTable _dataTable = GetDataTable();
            foreach (DataRow _row in _dataTable.Rows)
            {
                _pointCollection.Add(new Point(Convert.ToInt64((DateTime.Parse(_row["date"].ToString()).Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds), Convert.ToDouble(_row["value"])));
            }
            
            //Add data to serie
            var series = new Collection<Serie>();
            series.Add(new Serie() { data = _pointCollection.ToArray() });
            hcVendas.PlotOptions = new Core.PlotOptions.PlotOptionsLine { dataLabels = new Core.PlotOptions.DataLabels { enabled = true } };
            
            //Bind the control
            hcVendas.DataSource = series;
            hcVendas.DataBind();

        }

        private DataTable GetDataTable()
        {
            DataTable _dataTable = InitDataTable();
            DataRow _row = _dataTable.NewRow();
            _row["recordID"] = 1;
            _row["date"] = new DateTime(2011, 12, 1);
            _row["value"] = 5;
            _dataTable.Rows.Add(_row);
            
            _row = _dataTable.NewRow();
            _row["recordID"] = 2;
            _row["date"] = new DateTime(2011, 12, 2);
            _row["value"] = 7;
            _dataTable.Rows.Add(_row);

            _row = _dataTable.NewRow();
            _row["recordID"] = 3;
            _row["date"] = new DateTime(2011, 12, 3);
            _row["value"] = 9;
            _dataTable.Rows.Add(_row);

            _row = _dataTable.NewRow();
            _row["recordID"] = 4;
            _row["date"] = new DateTime(2011, 12, 4);
            _row["value"] = 8;
            _dataTable.Rows.Add(_row);

            _row = _dataTable.NewRow();
            _row["recordID"] = 5;
            _row["date"] = new DateTime(2011, 12, 5);
            _row["value"] = 10;
            _dataTable.Rows.Add(_row);

            return _dataTable;
        }

        private DataTable InitDataTable()
        {
            DataTable _dataTable = new DataTable();
            _dataTable.Columns.Add(new DataColumn("recordID", typeof(int)));
            _dataTable.Columns.Add(new DataColumn("date", typeof(DateTime)));
            _dataTable.Columns.Add(new DataColumn("value", typeof(double)));
            return _dataTable;
        }
    }
}
